-- 
-- SEQUENCE: CAL_RESERVA_SALON_SQ 
--
CREATE SEQUENCE CAL_RESERVA_SALON_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_RESERVA_SALON_TRG 
--
CREATE OR REPLACE TRIGGER CAL_RESERVA_SALON_TRG
BEFORE INSERT ON CAL_RESERVA_SALON
FOR EACH ROW

BEGIN
  SELECT CAL_RESERVA_SALON_SQ.NEXTVAL
  INTO   :new.Id_Temporada_Salon
  FROM   dual;
END;


-- 
-- SEQUENCE: CAL_CENTRO_SQ 
--
CREATE SEQUENCE CAL_CENTRO_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_CENTRO_TRG 
--
CREATE OR REPLACE TRIGGER CAL_CENTRO_TRG
BEFORE INSERT ON CAL_CENTRO
FOR EACH ROW

BEGIN
  SELECT CAL_CENTRO_SQ.NEXTVAL
  INTO   :new.id_centro
  FROM   dual;
END;


-- 
-- SEQUENCE: CAL_EDIFICIO_SQ
--
CREATE SEQUENCE CAL_EDIFICIO_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_EDIFICIO_TRG 
--
CREATE OR REPLACE TRIGGER CAL_EDIFICIO_TRG
BEFORE INSERT ON CAL_EDIFICIO
FOR EACH ROW

BEGIN
  SELECT CAL_EDIFICIO_SQ.NEXTVAL
  INTO   :new.id_edificio
  FROM   dual;
END;


-- 
-- SEQUENCE: CAL_PERSONA_SQ
--
CREATE SEQUENCE CAL_PERSONA_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_PERSONA_TRG 
--
CREATE OR REPLACE TRIGGER CAL_PERSONA_TRG
BEFORE INSERT ON CAL_PERSONA
FOR EACH ROW

BEGIN
  SELECT CAL_CENTRO_SQ.NEXTVAL
  INTO   :new.Id_Persona
  FROM   dual;
END;


-- 
-- SEQUENCE: CAL_PROFESOR_SQ 
--
CREATE SEQUENCE CAL_PROFESOR_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_PROFESOR_TRG 
--
CREATE OR REPLACE TRIGGER CAL_PROFESOR_TRG
BEFORE INSERT ON CAL_PROFESOR
FOR EACH ROW

BEGIN
  SELECT CAL_PROFESOR_SQ.NEXTVAL
  INTO   :new.Id_Profesor
  FROM   dual;
END;

-- 
-- SEQUENCE: CAL_SALON_SQ
--
CREATE SEQUENCE CAL_SALON_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_SALON_TRG 
--
CREATE OR REPLACE TRIGGER CAL_SALON_TRG
BEFORE INSERT ON CAL_SALON
FOR EACH ROW

BEGIN
  SELECT CAL_SALON_SQ.NEXTVAL
  INTO   :new.id_salon
  FROM   dual;
END;



-- 
-- SEQUENCE: CAL_PERSONA_PREASIGNACION_SQ
--
CREATE SEQUENCE CAL_PERSONA_PREASIGNACION_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_PERSONA_PREASIGNACION_TRG 
--
CREATE OR REPLACE TRIGGER CAL_PERSONA_PREASIGNACION_TRG
BEFORE INSERT ON CAL_PERSONA_PREASIGNACION
FOR EACH ROW

BEGIN
  SELECT CAL_PERSONA_PREASIGNACION_SQ.NEXTVAL
  INTO   :new.id_persona
  FROM   dual;
END;


-- 
-- SEQUENCE: CAL_ESTUDIANTE_SQ
--
CREATE SEQUENCE CAL_ESTUDIANTE_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_ESTUDIANTE_TRG 
--
CREATE OR REPLACE TRIGGER CAL_ESTUDIANTE_TRG
BEFORE INSERT ON CAL_ESTUDIANTE
FOR EACH ROW

BEGIN
  IF :new.carne IS NULL THEN
    SELECT CAL_ESTUDIANTE_SQ.NEXTVAL
    INTO   :new.carne
    FROM   dual;
  END IF;
END;

-- 
-- SEQUENCE: CAL_SALON_DISPONIBLE_DET_SQ
--
CREATE SEQUENCE CAL_SALON_DISPONIBLE_DET_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_SALON_DISPONIBLE_DET_TRG 
--
CREATE OR REPLACE TRIGGER CAL_SALON_DISPONIBLE_DET_TRG
BEFORE INSERT ON CAL_SALON_DISPONIBLE_DET
FOR EACH ROW

BEGIN
  SELECT CAL_SALON_DISPONIBLE_DET_SQ.NEXTVAL
  INTO   :new.id_horario
  FROM   dual;
END;

-- 
-- SEQUENCE: CAL_PLAN_SQ
--
CREATE SEQUENCE CAL_PLAN_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- SEQUENCE: CAL_PLAN_DETALLE_SQ
--
CREATE SEQUENCE CAL_PLAN_DETALLE_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;
  
-- 
-- TRIGGER: CAL_PLAN_DETALLE_TRG 
--
CREATE OR REPLACE TRIGGER CAL_PLAN_DETALLE_TRG
BEFORE INSERT ON CAL_PLAN_DETALLE
FOR EACH ROW

BEGIN
  SELECT CAL_PLAN_DETALLE_SQ.NEXTVAL
  INTO   :new.id_detalle
  FROM   dual;
END;


-- 
-- SEQUENCE: CAL_ACTA_NOTAS_SQ
--
CREATE SEQUENCE CAL_ACTA_NOTAS_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;
  
-- 
-- TRIGGER: CAL_ACTA_NOTAS_TRG 
--
CREATE OR REPLACE TRIGGER CAL_ACTA_NOTAS_TRG
BEFORE INSERT ON CAL_ACTA_NOTAS
FOR EACH ROW

BEGIN
  SELECT CAL_ACTA_NOTAS_SQ.NEXTVAL
  INTO   :new.id_acta
  FROM   dual;
END;

-- 
-- SEQUENCE: CAL_GESTION_SQ
--
CREATE SEQUENCE CAL_GESTION_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;
  
  
  -- 
-- SEQUENCE: CAL_INSCRIPCION_SQ
--
CREATE SEQUENCE CAL_INSCRIPCION_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_INSCRIPCION_TRG 
--
CREATE OR REPLACE TRIGGER CAL_INSCRIPCION_TRG
BEFORE INSERT ON CAL_INSCRIPCION
FOR EACH ROW

BEGIN
  SELECT CAL_INSCRIPCION_SQ.NEXTVAL
  INTO   :new.Id_Inscripcion
  FROM   dual;
END;

-- 
-- SEQUENCE: CAL_PRE_INSCRIPCION_SQ
--
CREATE SEQUENCE CAL_PRE_INSCRIPCION_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_PRE_INSCRIPCION_TRG 
--
CREATE OR REPLACE TRIGGER CAL_PRE_INSCRIPCION_TRG
BEFORE INSERT ON CAL_PRE_INSCRIPCION
FOR EACH ROW

BEGIN
  SELECT CAL_INSCRIPCION_SQ.NEXTVAL
  INTO   :new.Id_Pre_Inscripcion
  FROM   dual;
END;

-- 
-- SEQUENCE: CAL_TIPO_TEMPORADA_SQ
--
CREATE SEQUENCE CAL_TIPO_TEMPORADA_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_TIPO_TEMPORADA_TRG 
--
CREATE OR REPLACE TRIGGER CAL_TIPO_TEMPORADA_TRG 
BEFORE INSERT ON CAL_TIPO_TEMPORADA
FOR EACH ROW

BEGIN
  SELECT CAL_TIPO_TEMPORADA_SQ.NEXTVAL
  INTO   :new.Id_tipo_temporada
  FROM   dual;
END;


-- 
-- SEQUENCE: CAL_IDIOMA_SQ
--
CREATE SEQUENCE CAL_IDIOMA_SQ
  START WITH 1
  INCREMENT BY 1
  CACHE 100;

  
-- 
-- TRIGGER: CAL_IDIOMA_TRG 
--
CREATE OR REPLACE TRIGGER CAL_IDIOMA_TRG 
BEFORE INSERT ON CAL_IDIOMA
FOR EACH ROW

BEGIN
  SELECT CAL_IDIOMA_SQ.NEXTVAL
  INTO   :new.Id_Idioma
  FROM   dual;
END;


CREATE OR REPLACE TRIGGER CAL_USUARIO_ESTUDIANTE_TRG
AFTER INSERT ON CAL_ESTUDIANTE
FOR EACH ROW

BEGIN
  INSERT INTO usuario(usuario, apellidos, nombres, direccion, telefono, email, password, movil, tipo_usuario)
  SELECT cp.id_Persona, cp.apellidos, cp.nombres, cp.direccion, cp.telefono, cp.email, :new.carne, cp.movil, 'CALUSAC'
  FROM   cal_persona cp
  WHERE cp.id_persona = :new.id_persona;
  
  INSERT INTO usuario_perfil(tipo_usuario, usuario, perfil)
  SELECT 'CALUSAC',cp.id_Persona, 'estudiante_calusac'
  FROM   cal_persona cp
  WHERE cp.id_persona = :new.id_persona;
END;